﻿using ServerBase.Client;
using ServerBase.Config;
using ServerBase.Other;
using ServerBase.Protocol;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using UtilPublic.ApiDispatcher;
using UtilPublic.Serialization;
using UtilWpf;

namespace ToolGame
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            App.WdMain = this;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            绑定控制器(this);

            ColumnGridNav.Width = new GridLength(0);
            GridNav.Visibility = Visibility.Hidden;
        }

        #region 页面切换
        private void ButtonPage_Checked(object sender, RoutedEventArgs e)
        {
            var button = (RadioButton)sender;
            var pageName = button.Tag.ToString();
            PageOn(pageName);
        }
        public static Dictionary<string, Page> DictPage = new Dictionary<string, Page>();
        public void PageOn(string pageName)
        {
            if (DictPage.TryGetValue(pageName, out var page))
            {
                FrameMain.Content = page;
                return;
            }
            string uri = "ToolGame." + pageName;
            Type type = Type.GetType(uri);
            if (type != null)
            {
                object obj = type.Assembly.CreateInstance(uri);
                page = obj as Page;
                FrameMain.Content = page;
                DictPage[pageName] = page;
            }
        }
        #endregion

        #region 绑定控制器
        public static Dictionary<EGtAction, Action<G2B_Gt_Action>> DictGtAction = new Dictionary<EGtAction, Action<G2B_Gt_Action>>();
        public static Dictionary<string, Action> DictButtonClick = new Dictionary<string, Action>();
        public static void 绑定控制器<T>(T t)
        {
            #region 绑定控制器
            try
            {
                var dict = ApiDpProMgr.GetController(t);
                foreach (var kvp in dict)
                {
                    LunarClientDispatcher.BindEventHandler(kvp.Value, (EProtocolId)kvp.Key);
                }
            }
            catch (Exception ex)
            {
                App.DebugError($"DispatchApiRegister {ex.Message}");
            }
            #endregion

            #region 绑定控制器
            try
            {
                var dict = ApiDpPubMgr.GetControllerI<T, G2B_Gt_Action>(t);
                foreach (var kvp in dict)
                {
                    DictGtAction[(EGtAction)kvp.Key] = kvp.Value;
                }
            }
            catch (Exception ex)
            {
                App.DebugError($"DispatchApiRegister {ex.Message}");
            }
            #endregion
            #region 绑定控制器
            try
            {
                var dict = ApiDpPubMgr.GetControllerS(t);
                foreach (var kvp in dict)
                {
                    DictButtonClick[kvp.Key] = kvp.Value;
                }
            }
            catch (Exception ex)
            {
                App.DebugError($"DispatchApiRegister {ex.Message}");
            }
            #endregion
        }
        #endregion

        #region 按钮
        public void Button_Click_Public(object sender, RoutedEventArgs e)
        {
            var button = (Button)sender;
            if (!DictButtonClick.TryGetValue(button.Name, out var act))
            {
                App.DebugError($"未注册的按钮时间 {button.Name} {button.Content}");
                return;
            }
            act();
            return;
        }
        #endregion

        #region 返回
        [ApiDpPro((short)EProtocolId.G2B_GT_ACTION)]
        public short OnGtAction()
        {
            var Rsp = LunarClientDispatcher.OnMsg.To<G2B_Gt_Action>();
            App.DebugMsg(Rsp);

            if (!Rsp.Success)
            {
                App.DebugError(Rsp.Result);
                return 1;
            }
            App.DebugDebug("返回成功");
            try
            {
                var GtAction = (EGtAction)Enum.Parse(typeof(EGtAction), Rsp.GtAction);
                if (DictGtAction.TryGetValue(GtAction, out var act))
                {
                    act(Rsp);
                }
                else
                {
                    App.DebugInfo("操作成功");
                }
            }
            catch
            {
                App.DebugError(EProtocolResult.返回管理员数据错误); return 1;
            }
            return 0;
        }


        #endregion

        private void PagePlayer_Checked(object sender, RoutedEventArgs e)
        {

        }
    }
}
